package com.zmwx.oss.controller;

import com.zmwx.oss.entity.Result;
import com.zmwx.oss.service.FileService;
import com.zmwx.oss.util.MinioUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
//@RequestMapping("/oss")
public class FileController {
    @Value("${storage.service.type}")
    private String storageType;
    @Resource
    private MinioUtil minioUtil;
    @Resource
    private FileService fileService;
    @RequestMapping("/testMinio")
    public String testGetAllBusket(){
       try {
           String allBucket = minioUtil.getAllBucket().toString();
           return allBucket;
       }catch (Exception e){
           throw new RuntimeException(e);
       }
    }
    @RequestMapping("/testNacos")
    public  String testNacos(){

        return  storageType;
    }


    @RequestMapping("/GetAllBuckets")
    public String GetAllBuckets(){
        try {
            String allBucket = fileService.getAllBucket().toString();
            return allBucket;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/getUrl")
    public String getUrl(String bucketName, String objectName) throws Exception {
        return fileService.getUrl(bucketName, objectName);
    }
    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
        String url = fileService.uploadFile(uploadFile, bucket, objectName);
        return Result.ok(url);
    }
}
